package user.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import user.dao.BookAdminDao;
import user.dao.SystemAdminDao;
import user.dao.UserDao;
import user.db.DBConnection;
import user.model.BookAdmin;
import user.model.SystemAdmin;
import user.model.User;
import user.util.StringUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.UIManager;

public class LoginFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField userNameText;
	private JPasswordField passwordField;
	private JComboBox comboBox = new JComboBox();
	private DBConnection dbConnection = new DBConnection();
	private UserDao userDao = new UserDao();
	private static String userName = null; //定义name，便于返回到主页面使用
	private static String adminName = null;
	private static String password = null;//定义密码
	private static String id = null; //定义id，便于返回到主页面使用
	private static int bookid = 0;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrame frame = new LoginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		setBackground(Color.WHITE);
		setResizable(false);
		setTitle("登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1000, 785);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(215, 228, 242));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		ImageIcon icon = new ImageIcon("src/images/pictrue.png");
		JLabel background = new JLabel(icon);
		this.getLayeredPane().setLayout(null);
		background.setBounds(0,0,1000,785);
		this.getLayeredPane().add(background,new Integer(Integer.MIN_VALUE));
		
		JLabel Label1 = new JLabel("用户名");
		Label1.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		Label1.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/用户.png")));
		
		userNameText = new JTextField();
		userNameText.setFont(new Font("宋体", Font.PLAIN, 24));
		userNameText.setColumns(10);
		
		JLabel Label2 = new JLabel("密码");
		Label2.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		Label2.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/密码.png")));
		
		JLabel Label3 = new JLabel("权限");
		Label3.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/权限.png")));
		Label3.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		
		
		this.comboBox.setModel(new DefaultComboBoxModel(new String[] {"用户", "图书管理员", "系统管理员"}));
		this.comboBox.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		
		JButton Button2 = new JButton("登录");
		Button2.setFont(new Font("宋体", Font.PLAIN, 26));
		Button2.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				 LoginActionPerformed(e);
			}
		});
		
		Button2.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/登录设置.png")));
		
		JButton Button1 = new JButton("注册");
		Button1.setFont(new Font("宋体", Font.PLAIN, 26));
		Button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RegActionPerformed(e);
			}
		});
		Button1.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/注册.png")));
		
		JButton Button3 = new JButton("重置");
		Button3.setFont(new Font("宋体", Font.PLAIN, 26));
		Button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ResetActionPerformed(e);
			}
		});
		Button3.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/重置.png")));
		
		passwordField = new JPasswordField();
		passwordField.setFont(new Font("宋体", Font.PLAIN, 24));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap(178, Short.MAX_VALUE)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(Label1, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
									.addComponent(Label3)
									.addComponent(Label2)))
							.addGap(68))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(71)
							.addComponent(Button1, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(Button2, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
							.addGap(245)
							.addComponent(Button3, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(passwordField, Alignment.LEADING)
								.addComponent(userNameText, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
								.addComponent(comboBox, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(54, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(248, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(userNameText, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
						.addComponent(Label1, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
					.addGap(66)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Label2)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
					.addGap(67)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Label3)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(153)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Button3, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addComponent(Button2, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
						.addComponent(Button1, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE))
					.addGap(42))
		);
		this.setLocationRelativeTo(null);
		contentPane.setOpaque(false);
		contentPane.setLayout(gl_contentPane);
	}

	/**
	 * 对注册事件的处理
	 * @param e
	 */
	protected void RegActionPerformed(ActionEvent e) {
		this.dispose();//销毁当前窗体
		new RegFrame().setVisible(true);
		
	}

	/**
	 * 对重置事件的处理
	 * @param e
	 */
	protected void ResetActionPerformed(ActionEvent e) {
		reset();
	}
	
	/**
	 * 重置
	 */
	public void reset() {
		this.userNameText.setText("");
		this.passwordField.setText("");
	}
	
	/**
	 * 对登录事件的处理
	 * @param e
	 */
	protected void LoginActionPerformed(ActionEvent e) {
		String name = this.userNameText.getText().toString();
		String password = new String(this.passwordField.getPassword());
		if(StringUtil.isEmpty(name)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
			return;
		}
		if(StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
		    return;
		}
		
		Connection con = null;
		User user = new User();
		user.setUserName(name);
		user.setPassword(password);
		DBConnection dbConnection = new DBConnection();
		UserDao userDao = new UserDao();
		String box = (String)this.comboBox.getSelectedItem();
		//如果下拉框选的是用户
		if(box.equals("用户")) {//用户登录
			try {
				con = dbConnection.getConnection();
				User currentUser = userDao.login(con, user);
				if(currentUser !=null) {
					JOptionPane.showMessageDialog(null, "登录成功");					
					userName = currentUser.getUserName();
					this.password = currentUser.getPassword();
					id = currentUser.getId();
					this.dispose();//关闭当前登录界面
					new MainFrame().setVisible(true);
					
				} else {
					JOptionPane.showMessageDialog(null, "用户名或密码错误！");
				    }
				}
			 catch (Exception e2) {
				e2.printStackTrace();
			}
			
    	}
		BookAdminDao bookAdminDao = new BookAdminDao();
		BookAdmin bookAdminMessage = new BookAdmin();
		bookAdminMessage.setAdminName(name);			
		bookAdminMessage.setPassword(password);
				
		//如果下拉框选的是图书管理员
		if(box.equals("图书管理员")) {//图书管理员登录
				try {
				con = dbConnection.getConnection();				
				BookAdmin currentUser = bookAdminDao.bookAdminLogin(con,bookAdminMessage);					
				if(currentUser != null ) {
					JOptionPane.showMessageDialog(null, "图书管理员登录成功！");
					adminName = currentUser.getAdminName();
					this.password = currentUser.getPassword();
					id = currentUser.getAdminId();
					this.dispose();//关闭当前登录界面
					new BookAdminFrame().setVisible(true);
					
				} else {
					JOptionPane.showMessageDialog(null, "用户名或密码错误！");
				}
				}catch(Exception evt) {
					evt.printStackTrace();
				}
		}
		SystemAdminDao systemAdminDao = new SystemAdminDao();
		SystemAdmin systemAdminMeseeage = new SystemAdmin();
		systemAdminMeseeage.setAdminName(name);
		systemAdminMeseeage.setPassword(password);
		if(box.equals("系统管理员")) {//系统管理员登录
			try {
			con = dbConnection.getConnection();				
			SystemAdmin currentUser = systemAdminDao.systemAdminLogin(con,systemAdminMeseeage);					
			if(currentUser != null ) {
				JOptionPane.showMessageDialog(null, "系统管理员登录成功！");
				adminName = currentUser.getAdminName();
				this.password = currentUser.getPassword();
				id = currentUser.getAdminId();
				this.dispose();//关闭当前登录界面
				new SystemAdminFrame().setVisible(true);
				
			} else {
				JOptionPane.showMessageDialog(null, "用户名或密码错误！");
			}
			}catch(Exception evt) {
				evt.printStackTrace();
			}
	}
	}
	public static String userName() {
		return userName;
	}
	public static String adminName() {
		return adminName;
	}
	public static String password() {
		return password;
	}
	public static String id() {
		return id;
	}
	public static int bookid() {
		return bookid;
	}
}

